package com.alawn.framework.services.security;

import com.alawn.framework.core.domain.AuditableEntity;
import com.alawn.framework.services.enums.ResourceType;
import com.baomidou.mybatisplus.annotation.TableName;

import lombok.Getter;
import lombok.Setter;

/**
 * <p>
 * 资源表
 * </p>
 *
 * @author johnny
 * @since 2019-02-27
 */
@Getter
@Setter
@TableName("mz_a_resource")
public class Resource extends AuditableEntity {

	private static final long serialVersionUID = 1L;

	public Resource() {

	}

	public Resource(ResourceType type) {
		this.type = type;
	}

	/**
	 * 父id
	 */
	private Long parentId;

	/**
	 * 编码
	 */
	private String code;

	/**
	 * 名称
	 */
	private String name;

	/**
	 * 资源类型：0module、1page、2button
	 */
	private ResourceType type = ResourceType.MODULE;

	/**
	 * 访问路径
	 */
	private String url;

	/**
	 * 权限
	 */
	private String permission;

	/**
	 * 外部链接打开
	 */
	private String target;

	/**
	 * 图标
	 */
	private String icon;

	/**
	 * 资源禁用：0是、1否
	 */
	private boolean disable;

	/**
	 * 排序
	 */
	private Integer seq;

	/**
	 * 备注
	 */
	private String remark;

	public boolean isChildren(Resource parent) {
		if (parent == null) {
			throw new IllegalStateException("resource is not null");
		}
		if (this.parentId == parent.getId()) {
			return true;
		}
		if ((this.parentId != null && parent.getId() != null)
				&& (this.parentId.longValue() == parent.getId().longValue())) {
			return true;
		}
		return false;
	}

	public boolean isEnable() {
		return !this.disable;
	}

	public void setEnable(boolean enable) {
		this.disable = !enable;
	}
}
